package com.haikesi.api.resource.service.impl;

import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.ErrorTools;
import com.haikesi.api.base.Type;
import com.haikesi.api.resource.dao.ResourcesDao;
import com.haikesi.api.resource.domain.po.ResourcesPO;
import com.haikesi.api.resource.domain.vo.ResourecesFlagVO;
import com.haikesi.api.resource.domain.vo.ResourecesSavaVO;
import com.haikesi.api.resource.domain.vo.ResourecesSearchVO;
import com.haikesi.api.resource.domain.vo.ResourecesUpdateVO;
import com.haikesi.api.resource.service.ResoureService;
import com.haikesi.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**  
 * @ClassName:  ResourceServiceImpl 
 * @Description:  资源管理增删改成   资源配置的查询和添加     
 * @author: dx     
 * @date:   2019年4月28日 下午9:28:00   
 * @version V1.0 
 * @Copyright: 2019 重庆海克斯科技有限公司. 
 * 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */ 
@Service
@Transactional
public class ResourceServiceImpl implements ResoureService {
	@Autowired
	ResourcesDao resourcesDao;


	@Override
	public List<ResourecesFlagVO> getList(ResourecesSearchVO searchVO) {
		return resourcesDao.getList(searchVO);
	}

	@Override
	public String create(ResourecesSavaVO vo) {
		ResourcesPO po =new ResourcesPO();
		BeanUtils.copyProperties(vo, po);//将vo值赋给po
		po.preInsert();//初始化po

		Example example = new Example(ResourcesPO.class);
		example.and().andEqualTo("resourcesName", po.getResourcesName());
		ResourcesPO resourcesName = resourcesDao.selectOneByExample(example);
		if (resourcesName != null) {
			throw new BusinessException(Type.EXIST_ERROR, ErrorTools.ErrorAsArrayList(new Error("resourcesName", "该资源名称已存在")));
		}
		Example example1 = new Example(ResourcesPO.class);
		example1.and().andEqualTo("resourcesUrl", po.getResourcesUrl());
		ResourcesPO resourcesUrl = resourcesDao.selectOneByExample(example1);
		if (resourcesUrl != null) {
			throw new BusinessException(Type.EXIST_ERROR, ErrorTools.ErrorAsArrayList(new Error("resourcesUrl", "该资源路径已存在")));
		}

		int i=resourcesDao.insertSelective(po);
		if(i== Constants.SQL_RETUN_SUCCESS){
			throw new BusinessException(Type.EXCEPTION_FAIL);
		}
		return po.getId();
	}

	@Override
	public void update(ResourecesUpdateVO updateVO) {
		ResourcesPO po =new ResourcesPO();
		BeanUtils.copyProperties(updateVO, po);
		po.preUpdate();
		int i=resourcesDao.updateByPrimaryKeySelective(po);//会对字段进行判断再更新(如果为Null就忽略更新)，
		if(i== Constants.SQL_RETUN_SUCCESS){
			throw new BusinessException(Type.EXCEPTION_FAIL);
		}
		
	}
	@Override
	public void deleteResourceById(String id) {
//		resourcesDao.deleteResourceById(id);
		ResourcesPO po =new ResourcesPO();
        po.setId(id);
        po.preDelete();
		resourcesDao.updateByPrimaryKeySelective(po);
	}
	

	@Override
	public void deleteResource(List<String> ids) {
		resourcesDao.deleteResource(ids);
	}

	@Override
	public void insertResource(List<String> oldListId, String authorityId) {
		resourcesDao.insertResource(oldListId, authorityId);
	}



	
	

}
